package com.ustc.py.user.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ustc.py.user.entity.User;
import com.ustc.py.user.mapper.UserMapper;
import com.ustc.py.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;



@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private RestTemplate restTemplate;
    @Override
    public User handleLogin(String url, String nickname) {
        log.debug("{}-------{}",url,nickname);
        String response = restTemplate.getForObject(url, String.class);
        System.out.println(response+"-----------" +
                "");
        JSONObject jsonObject = JSONObject.parseObject(response);
        String openid = jsonObject.getString("openid");
        String sessionKey = jsonObject.getString("session_key");
        User user = query().eq("open_id", openid).one();
        if(user==null){
            user=new User();
            user.setOpenId(openid);
            user.setUsername(nickname);
            save(user);
        }
        return user;
    }
}
